package com.cy.jt.service;

import com.cy.jt.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        Map<String,Object> userMap = userMapper.selectUserByUsername(username);

        if(userMap==null)//假设这是从数据库查询的信息
            throw new UsernameNotFoundException("user not exists");

        List<String> authority = userMapper.qeruyUserPermissions((Long)userMap.get("id"));


        User user=new User(username,(String) userMap.get("password"),
                AuthorityUtils.createAuthorityList(authority.toArray(new String[]{})));
        return user;
    }
}
